﻿// (c) Copyright Max Kazarin.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

/// <reference path='../Common/jquery-1.3.2.js' />
// <reference path='../Common/MicrosoftAjax.debug.js' />

Type.registerNamespace("jQueryNET.Controls");

jQueryNET.Controls.DataControlBase = function jQueryNET$Controls$DataControlBase(element) {
	/// <summary>
	/// This class implement ComboBox control
	/// </summary>
	/// <param name='element' type='Sys.UI.DomElement' domElement='true'>
	/// DOM element the control is associated with
	/// </param>

	jQueryNET.Controls.DataControlBase.initializeBase(this, [element]);
}

function jQueryNET$Controls$DataControlBase$initialize() {
	/// <summary>
	/// Initialize the control
	/// </summary>

	jQueryNET.Controls.DataControlBase.callBaseMethod(this, "initialize");

	this._onSucceededHandler = Function.createDelegate(this, this._onSucceeded);
	this._onFailedHandler = Function.createDelegate(this, this._onFailed);
	this._receiveDataSynchronizer = Function.createSynchronizer(this, this._receiveDataSync);
}

function jQueryNET$Controls$DataControlBase$dispose() {
	/// <summary>
	/// Dispose the control
	/// </summary>

	jQueryNET.Controls.DataControlBase.callBaseMethod(this, "dispose");
}

function jQueryNET$Controls$DataControlBase$get_dataSourceId() {
	if (arguments.length !== 0) throw Error.parameterCount();

	return this._dataSourceId;
}

function jQueryNET$Controls$DataControlBase$set_dataSourceId(value) {
	var e = Function._validateParams(arguments, [{ name: "value", type: String, mayBeNull: false}]);
	if (e) throw e;

	this._dataSourceId = value;
}

function jQueryNET$Controls$DataControlBase$get_isLoading() {
	if (arguments.length !== 0) throw Error.parameterCount();

	return (this._dataSource != null && this._dataSource.get_isLoading());
}

function jQueryNET$Controls$DataControlBase$get_isLoaded() {
	if (arguments.length !== 0) throw Error.parameterCount();

	return (this._dataSource != null && this._dataSource.get_isLoaded());
}

function jQueryNET$Controls$DataControlBase$receiveData(params, clear) {
	/// <summary>
	/// Get data items from service
	/// </summary>

	this._receiveDataSynchronizer(params, clear);
}

function jQueryNET$Controls$DataControlBase$_onSucceeded(sender, eventArgs) {
	/// <summary>
	/// OnSucceeded event handler
	/// </summary>

	this._hideLoadingLayer();

	this._completeReceive(eventArgs);
	
	Function.unlock(this);
}

function jQueryNET$Controls$DataControlBase$_onFailed(sender, eventArgs) {
	/// <summary>
	/// OnFailed event handler
	/// </summary>

	this._hideLoadingLayer();

	this._failedReceive(eventArgs);

	Function.unlock(this);
}

function jQueryNET$Controls$DataControlBase$_receiveDataSync(params, clear) {
	if (!this._dataSource) {
		this._dataSource = $find(this.get_dataSourceId());

		if (!this._dataSource)
			return;
	}

	this._showLoadingLayer();
	if (clear)
		this._dataSource.clear();
	this._dataSource.fetch(params, this._onSucceededHandler, this._onFailedHandler);
}

function jQueryNET$Controls$DataControlBase$_showLoadingLayer() {
	throw new Error.notImplemented();
}

function jQueryNET$Controls$DataControlBase$_hideLoadingLayer() {
	throw new Error.notImplemented();
}

function jQueryNET$Controls$DataControlBase$_completeReceive(data) {
	throw new Error.notImplemented();
}

function jQueryNET$Controls$DataControlBase$_failedReceive(data) {
	alert('error=' + data);
}

jQueryNET.Controls.DataControlBase.prototype = {
	_dataSourceId: null,
	_dataSource: null,

	initialize: jQueryNET$Controls$DataControlBase$initialize,
	dispose: jQueryNET$Controls$DataControlBase$dispose,

	get_dataSourceId: jQueryNET$Controls$DataControlBase$get_dataSourceId,
	set_dataSourceId: jQueryNET$Controls$DataControlBase$set_dataSourceId,
	get_isLoading: jQueryNET$Controls$DataControlBase$get_isLoading,
	get_isLoaded: jQueryNET$Controls$DataControlBase$get_isLoaded,

	receiveData: jQueryNET$Controls$DataControlBase$receiveData,
	
	_onSucceeded: jQueryNET$Controls$DataControlBase$_onSucceeded,
	_onFailed: jQueryNET$Controls$DataControlBase$_onFailed,

	_receiveDataSync: jQueryNET$Controls$DataControlBase$_receiveDataSync,
	_showLoadingLayer: jQueryNET$Controls$DataControlBase$_showLoadingLayer,
	_hideLoadingLayer: jQueryNET$Controls$DataControlBase$_hideLoadingLayer,
	_completeReceive: jQueryNET$Controls$DataControlBase$_completeReceive,
	_failedReceive: jQueryNET$Controls$DataControlBase$_failedReceive
}

jQueryNET.Controls.DataControlBase.registerClass("jQueryNET.Controls.DataControlBase", jQueryNET.Controls.ControlBase);